package org.jeecg.modules.demo.jxcx.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.demo.ddyht.controller.DdygPiciController;
import org.jeecg.modules.demo.jxcx.Utils.RandomUtil;
import org.jeecg.modules.demo.jxcx.po.JxcCost;
import org.jeecg.modules.demo.jxcx.po.JxcCzrz;
import org.jeecg.modules.demo.jxcx.po.JxcSupplier;
import org.jeecg.modules.demo.jxcx.service.JxcCostService;
import org.jeecg.modules.demo.jxcx.service.JxcCzrzService;
import org.jeecg.modules.demo.jxcx.service.JxcSupplierService;
import org.jeecg.modules.demo.jxcx.Utils.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@RestController
@RequestMapping({"jxcCost"})
public class JxcCostController {
    private static Logger logger = LoggerFactory.getLogger(JxcCostController.class);
    @Resource
    private JxcCostService jxcCostService;
    @Resource
    private JxcSupplierService jxcSupplierService;
    @Resource
    private JxcCzrzService jxcCzrzService;

    public JxcCostController() {
    }

    @GetMapping
    public R selectAll(Page<JxcCost> page, JxcCost jxcCost) {
        return (new R()).ok(this.jxcCostService.page(page, new QueryWrapper(jxcCost)));
    }

    @PostMapping({"/queryall"})
    @ResponseBody
    public R queryall(@RequestBody JSONObject params) {
        String companyId = params.getString("companyId");
        return (new R()).ok(this.jxcCostService.getByConpanyId(companyId));
    }

    @PostMapping({"/likeall"})
    @ResponseBody
    public R likeall(@RequestBody JSONObject params) {
        String like = params.getString("like");
        String companyId = params.getString("companyId");
        return (new R()).ok(this.jxcCostService.getBylike(like, companyId));
    }

    @GetMapping({"{id}"})
    public R selectOne(@PathVariable Serializable id) {
        return (new R()).ok(this.jxcCostService.getById(id));
    }

    @GetMapping({"/cs"})
    public String cs() {
        logger.info("cs");
        return "ok";
    }

    @PostMapping({"add"})
    public R insert(@RequestBody JxcCost jxcCost) {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String formattedDateTime = now.format(formatter);
        String only1 = RandomUtil.generateRandomString();
        String id = now.format(formatter2) + only1;
        String seller = jxcCost.getBaozhuang();
        String companyId = jxcCost.getCompanyId();
        String ranjiao = jxcCost.getRanjiao();
        jxcCost.setId(id);
        jxcCost.setOther(formattedDateTime);
        JxcSupplier jxcSupplier = this.jxcSupplierService.getBy(seller, companyId);
        if (jxcSupplier == null) {
            JxcSupplier jxcSupplier1 = new JxcSupplier();
            jxcSupplier1.setId(id);
            jxcSupplier1.setCreateTime(formattedDateTime);
            jxcSupplier1.setName(seller);
            jxcSupplier1.setCompanyId(companyId);
            jxcSupplier1.setAmount(ranjiao);
            this.jxcSupplierService.save(jxcSupplier1);

            JxcCzrz jxcCzrz1 = new JxcCzrz();
            jxcCzrz1.setId(DdygPiciController.setczrzid(id));
            jxcCzrz1.setCompanyId(companyId);
            jxcCzrz1.setInfor("姓名:"+seller+"金额"+ranjiao);
            jxcCzrz1.setType("供应商新增");
            R add = jxcCzrzService.add(jxcCzrz1);
        } else {
            BigDecimal add1 = (new BigDecimal(jxcSupplier.getAmount())).add(new BigDecimal(ranjiao));
            jxcSupplier.setAmount(add1.toString());
            this.jxcSupplierService.updateById(jxcSupplier);

            JxcCzrz jxcCzrz1 = new JxcCzrz();
            jxcCzrz1.setId(DdygPiciController.setczrzid(id));
            jxcCzrz1.setCompanyId(companyId);
            jxcCzrz1.setInfor("姓名:"+seller+"金额+"+jxcSupplier.getAmount()+"->"+add1.toString());
            jxcCzrz1.setType("供应商修改");
            R add = jxcCzrzService.add(jxcCzrz1);
        }
        JxcCzrz jxcCzrz = new JxcCzrz();
        jxcCzrz.setId(DdygPiciController.setczrzid(id));
        jxcCzrz.setCompanyId(companyId);
        jxcCzrz.setInfor("交易方:"+seller+"金额"+ranjiao+"类型"+jxcCost.getFreight());
        jxcCzrz.setType("成本费用新增");
        R add = jxcCzrzService.add(jxcCzrz);

        return (new R()).ok(this.jxcCostService.save(jxcCost));
    }

    @PutMapping({"update"})
    public R update(@RequestBody JxcCost jxcCost) {
        logger.info("成本费用修改数据" + jxcCost.toString());
        JxcCost old = this.jxcCostService.getBy(jxcCost.getId(), jxcCost.getCompanyId());
        String id = jxcCost.getId();
        String oldAmount = old.getRanjiao();
        String newAmount = jxcCost.getRanjiao();
        String seller = jxcCost.getBaozhuang();
        String companyId = jxcCost.getCompanyId();
        BigDecimal subtract;
        JxcSupplier jxcSupplier;
        if ((new BigDecimal(newAmount)).compareTo(new BigDecimal(oldAmount)) > 0) {
            subtract = (new BigDecimal(newAmount)).subtract(new BigDecimal(oldAmount));
            jxcSupplier = this.jxcSupplierService.getBy(seller, companyId);
            jxcSupplier.setAmount(String.valueOf((new BigDecimal(jxcSupplier.getAmount())).add(subtract)));
            this.jxcSupplierService.updateById(jxcSupplier);

            JxcCzrz jxcCzrz1 = new JxcCzrz();
            jxcCzrz1.setId(DdygPiciController.setczrzid(id));
            jxcCzrz1.setCompanyId(companyId);
            jxcCzrz1.setInfor("姓名:"+seller+"金额+"+subtract.toString());
            jxcCzrz1.setType("供应商修改");
            R add = jxcCzrzService.add(jxcCzrz1);
        } else if ((new BigDecimal(newAmount)).compareTo(new BigDecimal(oldAmount)) < 0) {
            subtract = (new BigDecimal(oldAmount)).subtract(new BigDecimal(newAmount));
            jxcSupplier = this.jxcSupplierService.getBy(seller, companyId);
            jxcSupplier.setAmount(String.valueOf((new BigDecimal(jxcSupplier.getAmount())).subtract(subtract)));
            this.jxcSupplierService.updateById(jxcSupplier);

            JxcCzrz jxcCzrz1 = new JxcCzrz();
            jxcCzrz1.setId(DdygPiciController.setczrzid(id));
            jxcCzrz1.setCompanyId(companyId);
            jxcCzrz1.setInfor("姓名:"+seller+"金额-"+subtract.toString());
            jxcCzrz1.setType("供应商修改");
            R add = jxcCzrzService.add(jxcCzrz1);
        }

        JxcCzrz jxcCzrz = new JxcCzrz();
        jxcCzrz.setId(DdygPiciController.setczrzid(id));
        jxcCzrz.setCompanyId(companyId);
        jxcCzrz.setInfor("交易方:"+seller+"金额："+oldAmount+"->"+newAmount+"类型："+old.getFreight()+"->"+jxcCost.getFreight());
        jxcCzrz.setType("成本费用修改");
        R add = jxcCzrzService.add(jxcCzrz);
        return (new R()).ok(jxcCostService.updateById(jxcCost));
    }

    @DeleteMapping
    public R delete(@RequestBody JSONObject params) {
        logger.info("成本费用删除数据" + params.toString());
        JSONArray idList = params.getJSONArray("idList");
        String id = (String) idList.get(0);
        String companyId = params.getString("companyId");
        String seller = params.getString("seller");
        JxcSupplier jxcSupplier = this.jxcSupplierService.getBy(seller, companyId);

        JxcCzrz jxcCzrz = new JxcCzrz();
        jxcCzrz.setId(DdygPiciController.setczrzid(id));
        jxcCzrz.setCompanyId(companyId);
        jxcCzrz.setInfor("交易方:"+seller);
        jxcCzrz.setType("成本费用删除");
        R add = jxcCzrzService.add(jxcCzrz);

        if (jxcSupplier != null) {
            BigDecimal a = (new BigDecimal(jxcSupplier.getAmount())).subtract(new BigDecimal(params.getString("ranjiao")));
            jxcSupplier.setAmount(a.toString());
            this.jxcSupplierService.updateById(jxcSupplier);

            JxcCzrz jxcCzrz1 = new JxcCzrz();
            jxcCzrz1.setId(DdygPiciController.setczrzid(id));
            jxcCzrz1.setCompanyId(companyId);
            jxcCzrz1.setInfor("姓名:"+seller+"金额-"+a.toString());
            jxcCzrz1.setType("供应商修改");
            R add1 = jxcCzrzService.add(jxcCzrz1);
        }

        return (new R()).ok(this.jxcCostService.removeByIds(idList));
    }
}
